/**
 * @Author:Geln Yang
 * @Created:2008-8-3
 * $ Revision: 1.0 $
 * $ Author: Geln Yang $
 * $ Date: 2008-8-3 $
 */
package org.geln.web.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.geln.web.page.Page;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @Author:Geln Yang
 * @Created:2008-8-3
 */
public abstract class BaseHibernateService extends HibernateDaoSupport {

	protected int findNameQueryCount(final String queryName, final Object[] params) {
		return findCount(params, new QueryCallBack() {
			public Query createQuery(Session session) {
				return session.getNamedQuery(queryName);
			}
		
		});
	}

	protected int findHQLQueryCount(final String queryString, final Object[] params) {
		return findCount(params, new QueryCallBack() {
			public Query createQuery(Session session) {
				return session.createQuery(queryString);
			}
		
		});
	}
	
	protected List findNameQuery(final String queryName, final Object[] params, final Page page) {
		return find(params, page, new QueryCallBack() {
			public Query createQuery(Session session) {
				return session.getNamedQuery(queryName);
			}
		});
	}
	
	protected List findHQLQuery(final String queryString, final Object[] params, final Page page) {
		return find(params, page, new QueryCallBack() {
			public Query createQuery(Session session) {
				return session.createQuery(queryString);
			}
		});
	}
	
	protected List find(final Object[] params, final Page page,final QueryCallBack callBack) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = callBack.createQuery(session);
				if(page !=null && page.getPageSize()>0){
					int start = (page.getCurrentPage() - 1) * page.getPageSize();
					if (start >= page.getRecordCount())
						start = page.getRecordCount() - 1;
					query.setFirstResult(start);
					query.setMaxResults(page.getPageSize());   
				}
				if(params!=null){
					for(int i=0;i<params.length;i++){
						 query.setParameter(i, params[i]);
					 }
				}
				return query.list();
			}
		});
	}
	
	protected int findCount(final Object[] params,final QueryCallBack callBack) {
		Object value = getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = callBack.createQuery(session);
				if(params!=null){
					for(int i=0;i<params.length;i++){
						 query.setParameter(i, params[i]);
					 }
				}
				return query.uniqueResult();
			}
		});
		
		return Integer.parseInt(value.toString());
	}
	
	protected interface QueryCallBack {
		public Query createQuery(Session session);
	}

}